#!/bin/bash
  
###
### clean_dir.sh — 清理目录下的过期文件,默认深度为1
###
### Usage:
###   clean_dir.sh <dir> <day>
###
### Options:
###   <dir>  输入需要清理的目录的绝对路径，如: /opt/test.
###   <day>  输入需要清理几天前的数据(保留几天内的数据),如: 7.
###   -h     显示脚本说明.
###
### Example:
###   clean_dir.sh /test 7
###   说明: 清理test目录下7天以前的文件.

clean() {
        dir=$1
        day=$2
        protect_dir="/ bin /boot  /dev /etc /home /lib /lib64 /media /mnt /opt /proc /root /run /sbin /srv /sys /tmp /usr /var"
        if [[ $protect_dir =~ $dir ]];then
                echo -e "\033[40;31;5m $dir is Protected, Can't rm!!!!! \033[0m"
                exit 1
        fi
        find $dir -maxdepth 1 -type f -mtime +$day -exec rm -f {} \;
}

help() {
    sed -rn 's/^### ?//;T;p' "$0"
}


if [[ $# == 0 ]] || [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]]; then
        help
        exit 1
fi

clean $1 $2